package cn.tansanqinger.service.user.impl;

import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.List;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import cn.tansanqinger.dao.sql.user.UserMessageDispose;
import cn.tansanqinger.pojo.User;
import cn.tansanqinger.service.user.IUserMessageService;
import cn.tansanqinger.util.Encoder_Dispose;
import cn.tansanqinger.util.PageData;
/**
 * 处理用户的数据
 * @author C3H2 
 * @sine 9月
 */
@Service
public class UserMessageServiceImpl implements IUserMessageService{

	@Autowired
	private UserMessageDispose uDispose;
	
	@Override
	public boolean addUser(String email, String phone, String passwd) {
		if(Encoder_Dispose.isNull(email,phone,passwd)) {
			if(Encoder_Dispose.isEmail(email)&&Encoder_Dispose.isPhone(phone)) {
				User user = new User();
				user.setEmail(email);
				user.setPhone(phone);
				user.setPasswd(Encoder_Dispose.encoder(passwd));
				user.setCreatetime(new SimpleDateFormat("yyyy-MM-dd HH:mm:ss").format(new Date()));
				user.setImg("/img/default.png");
				if(uDispose.insert(user)>0) {
					return true;
				}
			}
		}
		return false;
	}
	
	@Override
	public boolean addTeacher(PageData pd) {
		if(Encoder_Dispose.isNull(pd.getString("email"),pd.getString("phone"),pd.getString("passwd1"))) {
			if(Encoder_Dispose.isEmail(pd.getString("email"))&&Encoder_Dispose.isPhone(pd.getString("phone"))) {
				User user = new User();
				user.setEmail(pd.getString("email"));
				user.setImg("/img/default.png");
				user.setPhone(pd.getString("phone"));
				user.setPasswd(Encoder_Dispose.encoder(pd.getString("passwd1")));
				user.setProjecturl(pd.getString("project_url"));
				user.setIdcard(pd.getString("count0"));
				user.setDiploma(pd.getString("count1"));
				user.setDegree(pd.getString("count2"));
				user.setTeachercertification(pd.getString("count3"));
				user.setCreatetime(new SimpleDateFormat("yyyy-MM-dd HH:mm:ss").format(new Date()));
				user.setJurisdiction(1);
				user.setIsactivate(0);
				user.setAccount((System.currentTimeMillis()+"").substring(3));
				if(uDispose.insert(user)>0) {
					return true;
				}
			}
		}
		return false;
	}

	@Override
	public boolean isEmail(String email) {
		if(Encoder_Dispose.isEmail(email)) {
			return uDispose.isEmail(email);
		}
		return false;
	}

	@Override
	public boolean isPhone(String phone) {
		if(Encoder_Dispose.isPhone(phone)) {
			return uDispose.isPhone(phone);
		}
		return false;
	}

	@Override
	public User selectOneEmail(String email) {
		if(Encoder_Dispose.isEmail(email)) {
			return uDispose.selectOneEmail(email);
		}
		return null;
	}

	@Override
	public User selectOnePhone(String phone) {
		if(Encoder_Dispose.isPhone(phone)) {
			return uDispose.selectOnePhone(phone);
		}
		return null;
	}

	@Override
	public List<User> selectAll() {
		return null;
	}
	
	@Override
	public boolean updatePasswd(String email, String passwd) {
		if(Encoder_Dispose.isNull(email,passwd)) {
			if(Encoder_Dispose.isEmail(email)) {
				User account = new User();
				account.setEmail(email);
				account.setPasswd(passwd);
				if(uDispose.updateOne(account, email)>0) {
					return true;
				}
				
			}
		}
		return false;
	}
	@Override
	public boolean updateOne(User account) {
		if(Encoder_Dispose.isEmail(account.getEmail())) {
			return uDispose.updateOne(account, account.getEmail())>0?true:false;
		}
		return false;
	}

	@Override
	public boolean delete(String email) {
		if(Encoder_Dispose.isEmail(email)) {
			if(uDispose.delete(email)>0) {
				return true;
			}
		}
		return false;
	}

	@Override
	public boolean updateById(User account) {
		return uDispose.updateById(account);
	}

}
